package com.fs.system.manager.service;

import com.fs.system.college_admin.domain.CollegeAdmin;
import com.fs.system.college_admin.mapper.CollegeAdminMapper;
import com.fs.system.manager.domain.Message;
import com.fs.system.manager.mapper.MessageMapper;
import com.fs.system.teacher.domain.Teacher;
import com.fs.system.teacher.mapper.TeacherMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private CollegeAdminMapper collegeAdminMapper;

    @Resource
    private TeacherMapper teacherMapper;

    public Message getMessageById(Long id) {
        return messageMapper.selectById(id);
    }

    public List<Message> getMessageByRoleId(Long roleId,String role) {
        return messageMapper.selectByRoleId(roleId,role);
    }

    public void teaAddSuccessMessToStu(Long stuId, Long teaId) {
        Teacher dbTeacher = teacherMapper.selectById(teaId);
        Message newMessage = new Message();
        newMessage.setType("初审通过通知");
        newMessage.setMessage("审核人："+dbTeacher.getRealname()+"，意见：初审通过！");
        newMessage.setStuId(stuId);  //收消息人
        newMessage.setRole("STUDENT");  //收信息人的角色
        newMessage.setCreateBy(dbTeacher.getRealname());  //发消息人
        newMessage.setCreateTime(LocalDateTime.now());
        messageMapper.insert(newMessage);
    }

    public void teaAddErrorMessToStu(Long stuId, Long teaId) {
        Teacher dbTeacher = teacherMapper.selectById(teaId);
        Message newMessage = new Message();
        newMessage.setType("初审打回通知");
        newMessage.setMessage("审核人："+dbTeacher.getRealname()+"，意见：初审打回！");
        newMessage.setStuId(stuId);  //收消息人
        newMessage.setRole("STUDENT");  //收信息人的角色
        newMessage.setCreateBy(dbTeacher.getRealname());  //发消息人
        newMessage.setCreateTime(LocalDateTime.now());
        messageMapper.insert(newMessage);
    }

    public void adminAddSuccessMessToStu(Long stuId, Long adminId) {
        CollegeAdmin dbCollegeAdmin = collegeAdminMapper.selectCollegeAdminById(adminId);
        Message newMessage = new Message();
        newMessage.setType("终审通过通知");
        newMessage.setMessage("审核人："+dbCollegeAdmin.getNickname()+"，意见：终审通过！");
        newMessage.setStuId(stuId);  //收消息人
        newMessage.setRole("STUDENT");  //收信息人的角色
        newMessage.setCreateBy(dbCollegeAdmin.getNickname());  //发消息人
        newMessage.setCreateTime(LocalDateTime.now());
        messageMapper.insert(newMessage);
    }

    public void adminAddSuccessMessToStuBatch(Long adminId, List<Long> stuIds) {
        for (Long stuId :stuIds) {
            this.adminAddSuccessMessToStu(stuId,adminId);
        }
    }

    public void adminAddErrorMessToStu(Long stuId, Long adminId) {
        CollegeAdmin dbCollegeAdmin = collegeAdminMapper.selectCollegeAdminById(adminId);
        Message newMessage = new Message();
        newMessage.setType("终审打回通知");
        newMessage.setMessage("审核人："+dbCollegeAdmin.getNickname()+"，意见：终审打回！");
        newMessage.setStuId(stuId);  //收消息人
        newMessage.setRole("STUDENT");  //收信息人的角色
        newMessage.setCreateBy(dbCollegeAdmin.getNickname());  //发消息人
        newMessage.setCreateTime(LocalDateTime.now());
        messageMapper.insert(newMessage);
    }

    public void adminAddErrorMessToStuBatch(Long adminId, List<Long> stuIds) {
        for (Long stuId :stuIds) {
            this.adminAddErrorMessToStu(stuId,adminId);
        }
    }

    public void adminAddGivenMessToTea(Long teaId, Long adminId) {
        CollegeAdmin dbCollegeAdmin = collegeAdminMapper.selectCollegeAdminById(adminId);
        Message newMessage = new Message();
        newMessage.setType("分配初审通知");
        newMessage.setMessage("分配人："+dbCollegeAdmin.getNickname()+"，意见：进行初审！");
        newMessage.setTeaId(teaId);  //收消息人
        newMessage.setRole("TEACHER");  //收信息人的角色
        newMessage.setCreateBy(dbCollegeAdmin.getNickname());  //发消息人
        newMessage.setCreateTime(LocalDateTime.now());
        messageMapper.insert(newMessage);
    }

    public void adminAddGivenMessToStu(Long stuId, Long adminId) {
        CollegeAdmin dbCollegeAdmin = collegeAdminMapper.selectCollegeAdminById(adminId);
        Message newMessage = new Message();
        newMessage.setType("等待初审通知");
        newMessage.setMessage("分配人："+dbCollegeAdmin.getNickname()+"，意见：等待初审！");
        newMessage.setTeaId(stuId);  //收消息人
        newMessage.setRole("STUDENT");  //收信息人的角色
        newMessage.setCreateBy(dbCollegeAdmin.getNickname());  //发消息人
        newMessage.setCreateTime(LocalDateTime.now());
        messageMapper.insert(newMessage);
    }
}
